﻿#include "ItemObject.h"
#include "../MirGraphics/MLibrary.h"
#include "../MirScenes/GameScene.h"
#include "../MirControls/MirLabel.h"

using namespace Client::MirGraphics;
using namespace Client::MirScenes;
namespace S = ServerPackets;

namespace Client::MirObjects
{

    ObjectType ItemObject::getRace() const
    {
        return ObjectType::Item;
    }

    bool ItemObject::getBlocking() const
    {
        return false;
    }

    ItemObject::ItemObject(unsigned int objectID) : MapObject(objectID)
    {
    }

    void ItemObject::Load(S::ObjectItem *info)
    {
        Name = info->Name;
        NameColour = info->NameColour;

        BodyLibrary = Libraries::FloorItems;

        CurrentLocation = info->Location;
        MapLocation = info->Location;
        GameScene::Scene->MapControl->AddObject(this);
        DrawFrame = info->Image;

        Size = BodyLibrary->GetTrueSize(DrawFrame);

        DrawY = CurrentLocation->Y;

    }

    void ItemObject::Load(S::ObjectGold *info)
    {
        Name = std::string::Format("Gold ({0:###,###,###})", info->Gold);


        BodyLibrary = Libraries::FloorItems;

        CurrentLocation = info->Location;
        MapLocation = info->Location;
        GameScene::Scene->MapControl->AddObject(this);

        if (info->Gold < 100)
        {
            DrawFrame = 112;
        }
        else if (info->Gold < 200)
        {
            DrawFrame = 113;
        }
        else if (info->Gold < 500)
        {
            DrawFrame = 114;
        }
        else if (info->Gold < 1000)
        {
            DrawFrame = 115;
        }
        else
        {
            DrawFrame = 116;
        }

        Size = BodyLibrary->GetTrueSize(DrawFrame);

        DrawY = CurrentLocation->Y;
    }

    void ItemObject::Draw()
    {
        if (BodyLibrary != nullptr)
        {
            BodyLibrary->Draw(DrawFrame, DrawLocation, DrawColour);
        }
    }

    void ItemObject::Process()
    {
        DrawLocation = new Point((CurrentLocation->X - User->Movement->X + MapControl::OffSetX) * MapControl::CellWidth, (CurrentLocation->Y - User->Movement->Y + MapControl::OffSetY) * MapControl::CellHeight);
        DrawLocation->Offset((MapControl::CellWidth - Size->Width) / 2, (MapControl::CellHeight - Size->Height) / 2);
        DrawLocation->Offset(User->OffSetMove);
        DrawLocation->Offset(getGlobalDisplayLocationOffset());
        FinalDrawLocation = DrawLocation;

        DisplayRectangle = new Rectangle(DrawLocation, Size);
    }

    bool ItemObject::MouseOver(Point *p)
    {
        return MapControl::getMapLocation() == CurrentLocation;
        // return DisplayRectangle.Contains(p);
    }

    void ItemObject::DrawName()
    {
        CreateLabel(Color::Transparent, false, true);

        if (NameLabel == nullptr)
        {
            return;
        }
        Point tempVar(DisplayRectangle->X + (DisplayRectangle->Width - NameLabel->getSize()->Width) / 2, DisplayRectangle->Y + (DisplayRectangle->Height - NameLabel->getSize()->Height) / 2 - 20);
        NameLabel->setLocation(&tempVar);
        NameLabel->Draw();
    }

    void ItemObject::DrawBehindEffects(bool effectsEnabled)
    {
    }

    void ItemObject::DrawEffects(bool effectsEnabled)
    {


    }

    void ItemObject::DrawName(int y)
    {
        CreateLabel(Color::FromArgb(100, 0, 24, 48), true, false);

        Point tempVar(DisplayRectangle->X + (DisplayRectangle->Width - NameLabel->getSize()->Width) / 2, DisplayRectangle->Y + y + (DisplayRectangle->Height - NameLabel->getSize()->Height) / 2 - 20);
        NameLabel->setLocation(&tempVar);
        NameLabel->Draw();
    }

    void ItemObject::CreateLabel(Color *backColour, bool border, bool outline)
    {
        NameLabel = nullptr;

        for (int i = 0; i < LabelList.size(); i++)
        {
            if (LabelList[i]->getText() != Name || LabelList[i]->getBorder() != border || LabelList[i]->getBackColour() != backColour || LabelList[i]->getForeColour() != NameColour || LabelList[i]->getOutLine() != outline)
            {
                continue;
            }
            NameLabel = LabelList[i];
            break;
        }
        if (NameLabel != nullptr && !NameLabel->isDisposed())
        {
            return;
        }

        NameLabel = new MirControls::MirLabel();
        NameLabel->setAutoSize(true);
        NameLabel->setBorderColour(Color::Black);
        NameLabel->setBackColour(backColour);
        NameLabel->setForeColour(NameColour);
        NameLabel->setOutLine(outline);
        NameLabel->setBorder(border);
        NameLabel->setText(Regex::Replace(Name, R"(\d+$)", ""));

        LabelList.push_back(NameLabel);
    }
}
